import os.path
import sys
from string import *
from MultiGenSqueeze import *

#overview
#read inputfile, format as needed.
#let w = input, v = processed, u = unprocessed input,  such that w = vu
#for some prefix k in u, find best match in v, allowing m changes.
#changes defined as inserts,replacements,deletions
#if none found, remove prefix from u and append to v.
#continue until u is empty.

def main():
    argCount = len(sys.argv)
    chunkSize = 12
    file1 = None
    file2 = None
    outputFile = ''
    mmratio = .1
    #check for command line args
    
    if argCount != 5:
        printUsage()
    else:
        chunkSize = int(sys.argv[3])
        mmratio = float(sys.argv[4])
        
    #open files
    f1 = openSequence(sys.argv[1])
    f2 = openSequence(sys.argv[2])
    file1 = f1.read()
    file2 = f2.read()
    inputFile = sys.argv[1]
    #read sequence
    mgs = MultiGenSqueeze(file1,file2,chunkSize)
    mgs.compress(mmratio)
    
    #open file for output
    output = open("compressed.txt","w")
    if output:
        output.write(mgs.encoded)
    
    f1.close()
    f2.close()
    output.close()
    print('File Compressed Successfully')
    print('Original size:' + str(len(mgs.getSequence(file1))))
    print('Compressed:' + str(len(mgs.encoded)))
    print('Compression Ratio:' + str(1 - float(len(mgs.encoded))/len(mgs.getSequence(file1))))

def printUsage():
    print "usage: gensqueeze.py <fileToCompress> <fileCompressAgainst> <chunk-size>"
    exit(1)

def openSequence(filename):
    if os.path.exists(filename):
        return open(filename)
    else:
        print "Cannot open file " + filename
        exit(1)
    
    
main()
